Editors = Editors or {}

--[[
sai定义可以查看源码src\server\game\AI\SmartScripts\SmartScriptMgr.h

]]--

local source_type_select = {
	{value = 0, name = "生物", tooltip = "entryorguid填写生物的id或者guid"},
	{value = 1, name = "物体", },
	{value = 2, name = "区域", },
	{value = 3, name = "event事件", },
	{value = 4, name = "交谈", },
	{value = 5, name = "任务", },
	{value = 6, name = "技能", },
	{value = 7, name = "传送", },
	{value = 8, name = "副本", },
	{value = 9, name = "行动列表", },
}

local defines = {
	-- 前缀  定义选项 定义参数对应	  参数数
	event	= { {},			{},			5,	"事件"},
	action 	= { {},			{},			6,	"行动"},
	target 	= { {},			{},			3,	"目标"},
}

function init()
	for prefix,define in pairs(defines) do
		local query = DBQuery("select * from "..prefix.."_type_information order by "..prefix.."_type", "uieditors")
		for _,v in pairs(query) do
			define[1][v[0]+1] = {value = v[0], name = v[1], tooltip = v[2]}
			local parameter = {}
			local parameterTooltip = {}
			for i=1,define[3] do
				parameter[i] = v[2+i]
				parameterTooltip[i] = v[2+define[3]+i]
			end
			define[2][v[0]] = {parameter, parameterTooltip}
		end
	end
end
init()


local function EventActionTargetChangeEvent(editorView, field, value, prefix)
	local define = defines[prefix]
	for i=1,define[3] do
		local Label = editorView:GetLabel(prefix.."_param"..i.."Label")
		local TextBox = editorView:GetTextBox(prefix.."_param"..i.."TextBox")
		local text = define[2][tonumber(value)]

		if (text[1][i] and text[1][i]~="") then
			Label.Content = text[1][i]
			TextBox.ToolTip = text[2][i]
		else
			Label.Content = define[4].."参数"..i
			TextBox.ToolTip = ""
		end
	end
end

table.insert(Editors, {
	Type = "mysql",				--必填,编辑器类型,以后可能会添加dbc的支持
	Database = "world",			--表格所在数据库
	Table = "smart_scripts",	--表格的名字
	Name = "smart_scripts",		--编辑器名字,用于显示在编辑器左上显示,唯一,同时也用于注册对象
	Fields = 
	{
		{
			{
				orientation = true,
				{field="entryorguid",name="entry或guid"},
				{field="source_type",name="源类型",type="Select", selects = source_type_select},
				{field="id",name="id"},
				{field="link",name="连接"},
				{field="comment",name="描述"},
			},
			{
				borderColor = "#567d00",
				{
					field="event_type",name="事件类型",isFilter = true,type="Select", selects = defines.event[1], 
					OnValueChange = function(editorView, field, value)
						EventActionTargetChangeEvent(editorView, field, value, "event")
					end
				},
				{
					orientation = true,
					{field="event_phase_mask",name="事件阶段掩码"},
					{field="event_chance",name="事件触发几率"},
					{field="event_flags",name="事件标记"},
				},
				{
					orientation = true,
					{field="event_param1",name="事件参数1"},
					{field="event_param2",name="事件参数2"},
					{field="event_param3",name="事件参数3"},
					{field="event_param4",name="事件参数4"},
					{field="event_param5",name="事件参数5"},
				},
			},
			
			{
				borderColor = "#9999FF",
				{field="action_type",name="行动类型",isFilter = true,type="Select", selects = defines.action[1], 
					OnValueChange = function(editorView, field, value)
						EventActionTargetChangeEvent(editorView, field, value, "action")
					end},
				{
					{
						orientation = true,
						{field="action_param1",name="行动参数1"},
						{field="action_param2",name="行动参数2"},
						{field="action_param3",name="行动参数3"},
						{field="action_param4",name="行动参数4"},
						{field="action_param5",name="行动参数5"},
						{field="action_param6",name="行动参数6"},
					},
				},
			},
			
			{
				borderColor = "#FFCCFF",
				{field="target_type",name="目标类型",isFilter = true,type="Select", selects = defines.target[1], 
					OnValueChange = function(editorView, field, value)
						EventActionTargetChangeEvent(editorView, field, value, "target")
					end},
				{
					orientation = true,
					{field="target_param1",name="目标参数1"},
					{field="target_param2",name="目标参数2"},
					{field="target_param3",name="目标参数3"},
					{field="target_param4",name="目标参数4"},
				},
				{
					orientation = true,
					{field="target_x",name="坐标X"},
					{field="target_y",name="坐标Y"},
					{field="target_z",name="坐标Z"},
					{field="target_o",name="坐标O"},
				},
			},
		},
	}
})

